/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/13
 * Time: 17:39
 * Description:
 */
package com.hangkai.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hangkai.reggie.common.BaseContext;
import com.hangkai.reggie.common.Result;
import com.hangkai.reggie.domain.ShoppingCart;
import com.hangkai.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public Result<List<ShoppingCart>> getList(HttpSession session){
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, session.getAttribute("user"));
//        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return Result.success(list);
    }

    /**
     * 添加菜品或套餐到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public Result<ShoppingCart> addInCart(@RequestBody ShoppingCart shoppingCart){
        //设置用户id 指明是谁的购物车
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查询当前菜品是否在购物车里
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        if(dishId != null){
            //当前提交的是菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
//            //也要对比口味是否相同 这里前端没写 就算了
//            lqw.eq(shoppingCart.getDishFlavor()!=null,ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        }else{
            //当前提交的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //SQL: select * from shopping_cart where user_id=? and dish_id=?and dish_flavor=?/category_id=?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        if(cartServiceOne != null){
            //如果已存在 则使数量+1
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return Result.success(cartServiceOne);
    }

    @PostMapping("/sub")
    public Result<String> subCart(@RequestBody ShoppingCart shoppingCart){
        //得到用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }else{
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品在购物车里的个数
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
        Integer number = cartServiceOne.getNumber();
        if(number > 1){
            cartServiceOne.setNumber(cartServiceOne.getNumber() - 1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            shoppingCartService.removeById(cartServiceOne);
        }
        return Result.success("移除商品成功");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public Result<String> cleanCart(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(lqw);
        return Result.success("清空成功");
    }
}
